Imports System.Collections
Imports System.Data.OracleClient

Public Class clsGnrtProCHedr

  Dim mStrScriptCode As String = ""
  Dim mCmdData As OracleCommand
  Dim mDrData As OracleDataReader
  Dim mDrStringIndc As OracleDataReader
  Dim mStrTableName As String = ""
  Dim clsobjFileGen As clsGnrtFile


  Public Function mFnGenerateProCHedr(ByVal aStrTableName As String _
                                      ) As Boolean
    Dim lStrCode As String = ""
    Dim lStrColName As String = ""
    Dim lStrTempColName As String = ""
    Dim lStrColDataType As String = ""
        Dim lStrVrblSuffix As String = ""
        Dim lStrTableName As String = ""
        Dim lStrTemp As String
    Dim lStrTmpTablename

    If mFnCheckTableValidity(aStrTableName) = False Then
      MessageBox.Show("Table doesnot exist", gCONST_MSG_HEADER)
      Exit Function
        End If

    mStrTableName = aStrTableName.ToUpper

    lStrTableName = aStrTableName.Substring((aStrTableName.IndexOf("_") + 1))
    lStrTableName = lStrTableName.Replace("_", " ")
    lStrTableName = StrConv(lStrTableName, VbStrConv.ProperCase)
    lStrTableName = lStrTableName.Replace(" ", "")
    lStrTableName = lStrTableName + "DB"

    lStrTmpTablename = aStrTableName.Substring((aStrTableName.IndexOf("_") + 1)) + "_DB"
    lStrTmpTablename = lStrTmpTablename.ToUpper

    clsobjFileGen = New clsGnrtFile()

        lStrCode = lStrCode + "/*************************************************************************/" + vbCrLf
        lStrCode = lStrCode + "/*              H E A D E R  I N F O R M A T I O N  B O X                */" + vbCrLf
        lStrCode = lStrCode + "/*************************************************************************/" + vbCrLf
        lStrCode = lStrCode + "/*                                                                       */" + vbCrLf
        lStrCode = lStrCode + "/*                    TCS CONFIDENTIAL INFORMATION                       */" + vbCrLf
        lStrCode = lStrCode + "/*                                                                       */" + vbCrLf
        lStrCode = lStrCode + "/*              COPYRIGHT (c) 2007 TCS All Rights Reserved               */" + vbCrLf
        lStrCode = lStrCode + "/*                                                                       */" + vbCrLf

        lStrCode = lStrCode + "/*  This module contains confidential and proprietary information of     */" + vbCrLf
        lStrCode = lStrCode + "/*  TCS, and any reproduction, disclosure or use in whole or in part     */" + vbCrLf
        lStrCode = lStrCode + "/*  is expressly forbidden except as may be specifically authorized      */" + vbCrLf
        lStrCode = lStrCode + "/*  by prior written agreement or permission of TCS.                     */" + vbCrLf
        lStrCode = lStrCode + "/*                                                                       */" + vbCrLf
        lStrCode = lStrCode + "/*************************************************************************/" + vbCrLf
        lStrCode = lStrCode + "/*************************************************************************/" + vbCrLf
        lStrCode = lStrCode + "/*   Author        : Code Generator                                      */" + vbCrLf
        lStrCode = lStrCode + "/*   Date          : " + Now.Date + Space(52 - CStr(Now.Date).Length) + "*/" + vbCrLf
        lStrCode = lStrCode + "/*   Synopsis      : This File contains the table structure              */" + vbCrLf
        lStrCode = lStrCode + "/*                   for " + mStrTableName + Space(48 - mStrTableName.Length) + "*/" + vbCrLf
        lStrCode = lStrCode + "/*                                                                       */" + vbCrLf
        lStrCode = lStrCode + "/*   Modifications : This code is generated and should not be modified   */" + vbCrLf
        lStrCode = lStrCode + "/*                   manually                                            */" + vbCrLf
        lStrCode = lStrCode + "/*************************************************************************/" + vbCrLf + vbCrLf

        lStrCode = lStrCode + "" + vbCrLf
    lStrCode = lStrCode + "#ifndef " + "_" + lStrTmpTablename + "_ " + vbCrLf
    lStrCode = lStrCode + "#define " + "_" + lStrTmpTablename + "_ " + vbCrLf
        lStrCode = lStrCode + "" + vbCrLf

        lStrCode = lStrCode + "#include " + Chr(34) + "GlobalDefs.h" + Chr(34) + vbCrLf
        lStrCode = lStrCode + "#include " + Chr(34) + "ErrorCodes.h" + Chr(34) + vbCrLf
        lStrCode = lStrCode + "" + vbCrLf

        lStrCode = lStrCode + "#pragma pack(4)" + vbCrLf
        lStrCode = lStrCode + "" + vbCrLf

    lStrCode = lStrCode + "struct " + lStrTmpTablename + vbCrLf
        lStrCode = lStrCode + "{" + vbCrLf

        mprGetTableCols()

        While mDrData.Read

            lStrColName = mDrData("TAB_COL_NAME")
            mprGetColName(lStrColName)

            lStrTempColName = lStrColName.Replace("_", " ")
            lStrTempColName = StrConv(lStrTempColName, VbStrConv.ProperCase)
            lStrTempColName = lStrTempColName.Replace(" ", "")

            lStrColDataType = StrConv(mDrData("TDCM_DATA_CTGRY"), VbStrConv.Uppercase)

            mfnRtrnVarPrfix(mDrData("TDCM_BACK_END_DATA_TYPE"), lStrVrblSuffix)

      If lStrVrblSuffix <> "ca" Then
        lStrCode = lStrCode + Space(4) + lStrColDataType + " " + lStrVrblSuffix + lStrTempColName + ";" + vbCrLf
      Else
        lStrCode = lStrCode + Space(4) + lStrColDataType + " " + lStrVrblSuffix + lStrTempColName + "[" + lStrColDataType + "_LNGTH];" + vbCrLf
      End If


    End While

        lStrCode = lStrCode + "};" + vbCrLf
        lStrCode = lStrCode + "" + vbCrLf

        lStrCode = lStrCode + "#pragma pack()" + vbCrLf
        lStrCode = lStrCode + "" + vbCrLf

    lStrTemp = "typedef struct " + lStrTmpTablename + " " + lStrTmpTablename + " ;"
        If lStrTemp.Length > 70 Then
      lStrCode = lStrCode + "typedef struct " + lStrTmpTablename + vbCrLf + "      " + lStrTmpTablename + " ;" + vbCrLf
        Else
      lStrCode = lStrCode + "typedef struct " + lStrTmpTablename + " " + lStrTmpTablename + " ;" + vbCrLf
        End If
        lStrCode = lStrCode + "" + vbCrLf

        lStrCode = lStrCode + "#endif" + vbCrLf

    clsobjFileGen.mprGnrtProCHedrFile(lStrCode, lStrTableName, "ProC Files")

  End Function


    Private Function mprGetColName(ByRef aStrColName As String)
        Dim lIntPos As Integer

        lIntPos = aStrColName.IndexOf("_")
        aStrColName = aStrColName.Substring(lIntPos + 1)

    End Function


  Private Function mFnCheckTableValidity(ByVal aStrTableName As String) As Boolean

    Dim lStrQuery As String = ""
    Dim lIntCount As Integer

    lStrQuery = "select count(*) as CNT from TRDX_TABLES_MSTR where TABM_TABLE_NAME = UPPER('"
    lStrQuery = lStrQuery + aStrTableName + "')"

    mCmdData = New OracleCommand(lStrQuery, con)
    lIntCount = mCmdData.ExecuteScalar

    If lIntCount = 0 Then
      Return False
    Else
      Return True
    End If

  End Function




  Private Sub mprGetTableCols()

    Dim lStrQuery As String = ""

        lStrQuery = "select distinct TAB_COL_NAME , TAB_COL_SEQ_NO, TDCM_DATA_CTGRY, TDCM_BACK_END_DATA_TYPE from "
        lStrQuery = lStrQuery + "TRDX_TABLE_COLUMNS, TRDX_DATA_CATEGORY_MSTR, TRDX_APLCTN_VRBLS where TAB_TABLE_ID = ("
    lStrQuery = lStrQuery + "select distinct TABM_TABLE_ID from TRDX_TABLES_MSTR where "
    lStrQuery = lStrQuery + "TABM_TABLE_NAME = upper('"
        lStrQuery = lStrQuery + mStrTableName + "')) and TAB_VRBL_SERIAL_NMBR = TAV_SERIAL_NMBR"
        lStrQuery = lStrQuery + " AND  TAV_DATA_CTGRY = TDCM_DATA_CTGRY order by TAB_COL_SEQ_NO ASC"
    mCmdData = New OracleCommand(lStrQuery, con)
    mDrData = mCmdData.ExecuteReader

  End Sub





  Private Function mfnRtrnVarPrfix(ByVal aDataType As String _
                                 , ByRef aReturnType As String)

    Dim lPassResult As String = ""

    Select Case LCase((aDataType))

      Case ("double")
        lPassResult = "d"
      Case ("char array")
                lPassResult = "ca"
      Case ("long")
        lPassResult = "l"
      Case ("int")
        lPassResult = "i"
      Case ("date")
        lPassResult = "dt"
      Case Else
        lPassResult = ""
    End Select

    aReturnType = lPassResult

  End Function



End Class
